function createJson() {

    if (!checkDataAuth()) {
        return;
    }

    var ztreeNode = fileZtree.getSelectedNodes()[0];
    var currentPath = ztreeNode.path;

    var jsonType = "page";
    if (currentPath.indexOf(".flow") > -1) {
        jsonType = "flow";
    }
    if (currentPath.indexOf(".json") > -1) {
        currentPath = currentPath.substr(0, currentPath.lastIndexOf("/"));
        ztreeNode = ztreeNode.getParentNode();
    }

    var defaultPath = currentPath += "/xxx." + jsonType + ".json";
    var path = prompt(
        "\n后缀说明：\n" +
        "xxx.page.json 页面\n" +
        "yyy.flow.json 流程\n" +
        "默认JSON文件\n\n" +
        "文件名称：",
        defaultPath);
    if (!path) {
        return;
    }

    if (path.indexOf(".json") === -1) {
        alert("必须以.json结尾");
        return;
    }
    if (!path) {
        return;
    }

    path = path.replace(/[^\/\u4e00-\u9fa5_a-zA-Z0-9.-]/g, "");

    if (!path) {
        alert("代码名称不能为空！(只允许数字、字母、下划线、中文)");
        return;
    }

    let fileName = path.substr(path.lastIndexOf("/") + 1);

    let savePath = getRealJsonPath(path);

    saveFile(savePath, function () {
        //如果是新目录，就刷新页面 TODO
        if (currentPath.indexOf(path.substring(0, path.lastIndexOf("/"))) === -1) {
            window.location.reload();
            return;
        }
        var newNodes = [{ name: fileName, icon: "/common/icon/" + getIcon(path), path: path }];
        newNodes = fileZtree.addNodes(ztreeNode, newNodes);
        alert("保存成功！");
        fileZtree.selectNode(newNodes[0]);
        clickFileTree(null, null, newNodes[0]);
    });
}

function saveFile(path, callback) {

    if (!checkDataAuth()) {
        return;
    }
    var token = "";
    if (path.indexOf("/core/api") > -1) {
        //token校验
        token = inputToken();
        if (!token) {
            return;
        }
    }

    var json = {
    };

    if (path.indexOf(".form.json") > -1 || path.indexOf(".page.json") > -1) {
        json = {
            "schema": {
                "type": "object",
                "properties": {}
            }
        };
    }

    var savePath = "/core/api/file/save.flow.json/run";
    var input = { "path": path, "json": json };

    executeByPath(savePath, input, { token }, function (data) {
        callback(data);
    });
}

function editJson() {

    if (!checkDataAuth()) {
        return;
    }
    var treeNode = fileZtree.getSelectedNodes()[0];
    let jsonPath = getRealJsonPath(treeNode.path);
    addTab(treeNode.name + ".json", jsonPath + "/jsonEdit");
}

function downloadJson() {
    var treeNode = fileZtree.getSelectedNodes()[0];
    let jsonPath = getRealJsonPath(treeNode.path);

    window.open(jsonPath + "/download");
}

function getJsonPath() {
    var treeNode = fileZtree.getSelectedNodes()[0];
    let jsonPath = getRealJsonPath(treeNode.path);
    return jsonPath;
}

function getJsonDir() {
    let jsonPath = getJsonPath();
    let jsonDir = jsonPath.indexOf(".json") > -1 ? jsonPath.substr(0, jsonPath.lastIndexOf("/")) : jsonPath;
    return jsonDir;
}

function clickUploadJson() {
    $("#uploadFileInput").click();
}

function uploadJson(event) {
    var input = event.target;
    let fileName = input.files[0]
    let jsonDir = getJsonDir();
    let uploadPath = jsonDir + "/" + fileName.name + "/upload";
    var reader = new FileReader();

    reader.onload = function () {
        if (reader.result) {
            request(uploadPath, JSON.parse(reader.result), function (result) {
                alert(result.message);
                window.location.reload(); //TODO 定位的到上传文件
            });
        }
    };
    reader.readAsText(fileName);
};

function formEditor() {
    var url = "https://x-render.gitee.io/schema-generator/playground";
    addTab("表单编辑", url);
}

function deleteJson() {

    if (!checkDataAuth()) {
        return;
    }
    //token校验
    var token = inputToken();
    if (!token) {
        return;
    }

    var selectedNode = fileZtree.getSelectedNodes()[0];

    let jsonPath = getRealJsonPath(selectedNode.path);
    var input = {
        token: token,
        path: jsonPath
    };

    var servicePath = "/core/api/file/delete.flow.json/run";

    executeByPath(servicePath, input, { token }, function (data) {
        if (data.output.success === false) {
            alert("删除失败：" + JSON.stringify(data.output.message))
        } else {
            alert("删除成功！");
            fileZtree.removeNode(selectedNode);
        }
    });
}

function hideTree() {
    $('#main-layout').layout('collapse', 'west');
    setTimeout(() => {
        $('#operateContainer').tabs('resize');
    }, 100);
}

function copyPath() {
    let jsonPath = getJsonPath();

    const input = document.createElement('input');
    document.body.appendChild(input);
    input.value = jsonPath;  // 复制到剪切板的内容
    input.select();
    if (document.execCommand) {
        document.execCommand('copy');
        document.body.removeChild(input);
        alert('复制路径成功：\r\n' + jsonPath);
    } else {
        console.error('当前浏览器不支持copy');
        document.body.removeChild(input);
    }
}